<?php
namespace Admin\Controller;
use Think\Controller;
class ActivityController extends BaseController {


  public function activitylist(){
    $activitymodel=M('activity');
        $pageSize=5;//每页显示的数据个数
        $records=$activitymodel->count(); //查询数据表中数据的总个数
        $Page = new \Think\Page($records,$pageSize);//实例化分页类，传入总数和每页显示的数量
        $list=$activitymodel->order('activity_id desc')->limit($Page->firstRow,$pageSize)->select();//进行分页数据查询
        $page1=$Page->show();//分页显示输出
        $this->assign(array('activity'=>$list,'page1'=>$page1));//用数组的方式赋值数据集，赋值分页输出
    $this->display();
  }


        public function activityadd(){
        $activitymodel = M("activity");
        // $data['activity_pic'] = I('post.pic');
        $data['activity_name'] = I('post.name');
        $data['activity_date'] = I('post.date');
        $data['activity_address'] = I('post.address');
        $data['activity_explain'] = I('post.explain');
        $data['activity_phone'] = I('post.phone');
        $data['activity_price'] = I('post.price');
        if ($_FILES['file']['error'] == 0) {
            $logoinfo = $this->upload();
            $pname = $logoinfo['file']['savename'];
        } else {
            $pname = "default.jpg";
        }
        $data['activity_pic'] = $pname;
        $res = $activitymodel->add($data);
        if (false !== $res){
            $this->success('添加成功','http://localhost/PHPShiZhanDiLiuZu/yuejian6/Admin/Activity/activitylist.html');
        }else{
            $this->error('信息填写有误');
        }
    }

        public function getdata(){
        return date('Y-m-d H:i:s');
    }


    public function upload(){
        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
        $upload->rootPath  = './Public/pic/'; 
        $upload->autoSub = false;
        $info   =   $upload->upload();  
        if(!$info) {
            $this->error($upload->getError());
        }
        else{
            return $info;
        }                    
    }

        public function alteractivity($id){
          $activitymodel=M('activity');
          $activity=$activitymodel->where("activity_id=$id")->select();
          $this->activity=$activity;
          session('activity_id',$id);
          $this->display();
    }

    public function update(){
      $id['activity_id'] = I('session.activity_id');
      $activitymodel = M("activity");
      $data['activity_name'] = I('post.name');
      $data['activity_date'] = I('post.date');
      $data['activity_address'] = I('post.address');
      $data['activity_explain'] = I('post.explain');
      $data['activity_phone'] = I('post.phone');
      $data['activity_price'] = I('post.price');
      if ($_FILES['file']['error'] == 0) {
          $logoinfo = $this->upload();
          $pname = $logoinfo['file']['savename'];
      } else {
          $pname = "default.jpg";
      }
        $data['activity_pic'] = $pname;
        $res = $activitymodel->where($id)->save($data);
        if (false !== $res){
            $this->success('修改成功','http://localhost/PHPShiZhanDiLiuZu/yuejian6/Admin/Activity/activitylist.html');
        }else{
            $this->error('信息填写有误');
        }
    }

public function delactivity($activity_id){
    $activityModel= M('activity');
    $activity=$activityModel->where("activity_id=$activity_id")->find();
          if ($activity["activity_pic"]=="default.jpg") 
        {

        }
            else
        {
                unlink(realpath("./Public/pic/".$activity["activity_pic"]));
        }

    if (false!==$activityModel->where("activity_id=$activity_id")->delete()) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败');
        }
    }

 
}?>